const http = require('http')
const fs = require('fs')
const path = require('path')

let server = http.createServer()

server.listen(3001, () => {
    console.log('服务器启动成功: http://127.0.0.1:3001');
})
// request: 请求
// response: 响应
// require: 引入，需要
// read: 读取
// write: 写入
server.on('request', (req, res) => {
    console.log(req.url)
    // path.extname(req.url)

    fs.readFile(path.join(__dirname, 'views', req.url), (err, data) => {
        if (err) {
            fs.readFile(path.join(__dirname, 'views', '404.html'), (err, data) => {
                if (err) {
                    res.end('错误')
                } else {
                    res.statusCode = 404
                    res.end(data)
                }
            })
        } else {
            res.end(data)
        }
    })

})